package com.client;

//import com.client.UI_Stuff.*;
import com.google.gwt.core.client.*;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.Window;


/**
 * StringReverser
 *
 * This is the entry point class for the StringReverser module.
 * The entry point class will be used to load the GWT UI elements that will allow a user to enter a string and send it
 * to the StringReverser service
 *
 * @author Masud Idris
 * @version 1.1
 * @since Dec 6, 2007, 11:33:37 AM
 *
 */
public class StringReverser implements EntryPoint {
  private RootPanel mainPanel = RootPanel.get("String-Reverser-Panel");
  private TextBox inputString = new TextBox();
  private StringReverserServiceAsync proxy = GWT.create(StringReverserService.class);


  /**
   * This is the implementation of the onModuleLoad method. This method will be called when the StringReverser module
   * is loaded.
   */
  public void onModuleLoad() {
    // Clear the panel before adding UI elements
    //DOM.setInnerHTML(mainPanel.getElement(), "");

    //mainPanel.add(new StringReverserWidget());
	  mainPanel.add(inputString);
	  //RootPanel.add(mainPanel);
	  
	  inputString.addKeyPressHandler(new KeyPressHandler() {
	      public void onKeyPress(KeyPressEvent event) {
	        if (event.getCharCode() == KeyCodes.KEY_ENTER) {
	          reverseInput(inputString.getText());
	        }
	      }
	    });

  }
  
  public void reverseInput(String str){
	  if( proxy == null){
		  proxy = GWT.create(StringReverserService.class);
	  }
	  
	  ((ServiceDefTarget)proxy).setServiceEntryPoint(GWT.getModuleBaseURL() + "greet");
	  
	// Set up the callback object.
	   AsyncCallback<String> callback = new AsyncCallback<String>() {
	      
		   public void onFailure(Throwable caught) {
	    	  Window.alert(GWT.getModuleBaseURL() + "" + "Error" + caught);
	        // TODO: Do something with errors.
	      }

	      public void onSuccess(String result) {
	        //updateTable(result);
	    	  Window.alert(result);
	    	  inputString.setText(result);
	      }
	   };
	      
	   // Make the call to the stock price service.
	      proxy.reverseString(str, callback);
	   
  }
}
